﻿<template>
	<div class="equipTag-container">
		<el-dialog v-model="isShowDialog" :title="props.title" :width="700" draggable="">
			<el-form :model="ruleForm" ref="ruleFormRef" size="default" label-width="100px" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="名称" prop="name">
							<el-input v-model="ruleForm.name" placeholder="请输入名称" clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="点位编号" prop="tagnum">
							<el-input v-model="ruleForm.tagnum" placeholder="请输入点位编号" clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="编号属性" prop="tagproperty">
							<el-input v-model="ruleForm.tagproperty" placeholder="请输入编号属性" clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="地址" prop="address">
							<el-input  v-model="ruleForm.address" placeholder="请输入地址" clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item  label="数据类型" prop="datatype">
							
							<el-select filterable v-model="ruleForm.datatype" class="m-2" placeholder="选择" >
							<el-option v-for="item in dataTypes" :key="item.item2" :label="item.item1" :value="parseInt(item.item2)" />
						    </el-select>
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="数据长度" prop="datalength">
							<el-input-number min="0" v-model="ruleForm.datalength" placeholder="请输入数据长度" clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="设备" prop="equipinfoid">
							
							<el-input v-model="ruleForm.equipname" @click='openEquip' placeholder="请选择设备" clearable />
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="读写" prop="readwrite">
							<el-select v-model="ruleForm.readwrite" class="m-2" placeholder="选择" >
							<el-option  label="只读" :value="0" />
							<el-option  label="读写" :value="1" />
						    </el-select>
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="读取块" prop="readblock">		
							<el-input v-model="ruleForm.readblock" placeholder="请输入" clearable />				
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="是否启用" prop="isenable">
							<el-select v-model="ruleForm.isenable" class="m-2" placeholder="选择" >
							<el-option  label="启用" :value="1" />
							<el-option  label="禁用" :value="0" />
						    </el-select>
							
						</el-form-item>
						
					</el-col>
					<!--
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="Mqtt发布" prop="ismqttpublish">
							<el-select v-model="ruleForm.ismqttpublish" class="m-2" placeholder="选择" >
							<el-option  label="是" :value="1" />
							<el-option  label="否" :value="0" />
						    </el-select>
							
						</el-form-item>
						
					</el-col>
					-->
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="表达式" prop="expression">
							<el-input v-model="ruleForm.expression" placeholder="请输入表达式" clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="备注" prop="remark">
							<el-input v-model="ruleForm.remark" placeholder="请输入备注" clearable />
							
						</el-form-item>
						
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel" size="default">取 消</el-button>
					<el-button type="primary" @click="submit" size="default">确 定</el-button>
				</span>
			</template>
		</el-dialog>
		<equipInfoModal ref='infoModal' @close='closeInfoModal'/>
	</div>
</template>

<script lang="ts" setup>
	import { ref,onMounted } from "vue";
	import { ElMessage } from "element-plus";
	import type { FormRules } from "element-plus";
	import { addEquipTag, updateEquipTag,getEquipTagTypes } from "/@/api/main/equipTag";
	import equipInfoModal from '/@/views/main/equipInfo/component/equipInfoModal.vue'
	//父级传递来的参数
	var props = defineProps({
	title: {
	type: String,
	default: "",
	},
	});
	const infoModal=ref();
	const dataTypes=ref<any>([]);
	//父级传递来的函数，用于回调
	const emit = defineEmits(["reloadTable"]);
	const ruleFormRef = ref();
	const isShowDialog = ref(false);
	const ruleForm = ref<any>({});
		//自行删除非必填规则
		const rules = ref<FormRules>({
  name: [{required: true, message: '请输入名称！', trigger: 'blur',},],
  tagnum: [{required: true, message: '请输入点位编号！', trigger: 'blur',},],
  tagproperty: [{required: true, message: '请输入编号属性！', trigger: 'blur',},],
  address: [{required: true, message: '请输入地址！', trigger: 'blur',},],
  datatype: [{required: true, message: '请输入数据类型！', trigger: 'blur',},],
  datalenth: [{required: true, message: '请输入数据长度！', trigger: 'blur',},],
  equipinfoid: [{required: true, message: '请选择设备！', trigger: 'blur',},],
  
 
  isenable: [{required: true, message: '请输入是否启用！', trigger: 'blur',},],
 
});

// 打开弹窗
const openDialog = (row: any) => {
  ruleForm.value = JSON.parse(JSON.stringify(row));

  isShowDialog.value = true;

  
};

// 关闭弹窗
const closeDialog = () => {
  emit("reloadTable");
  isShowDialog.value = false;
};

// 取消
const cancel = () => {
  isShowDialog.value = false;
};

// 提交
const submit = async () => {
  ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
    if (isValid) {
      let values = ruleForm.value;
      if (ruleForm.value.id != undefined && ruleForm.value.id > 0) {
        await updateEquipTag(values);
      } else {
        await addEquipTag(values);
      }
      closeDialog();
    } else {
      ElMessage({
        message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
        type: "error",
      });
    }
  });
};

const closeInfoModal=(row)=>{

ruleForm.value.equipname=row.name;
ruleForm.value.equipinfoid=row.id;
}

const openEquip=()=>{
	infoModal.value.open();
}

// 页面加载时
onMounted(async () => {
	
	await iniData();
});

const iniData=async()=>{

	
	var rep=await getEquipTagTypes();
	dataTypes.value=rep.data.result;
}

//将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>




